//
//  BorderButton.m
//  CLExtension
//
//  Created by 廖朝瑞 on 2018/3/30.
//  Copyright © 2018年 Charlie. All rights reserved.
//

#import "BorderButton.h"

@interface BorderButton() {
    UIColor *_borderColor;
    UIColor *_borderColorPress;
    UIColor *_borderColorDisabled;
}

@end

@implementation BorderButton

- (UIColor *)borderColor {
    if (_borderColor)
        return _borderColor;
    else
        return self.tintColor;
}

- (UIColor *)borderColorPress {
    if (_borderColorPress)
        return _borderColorPress;
    else
        return self.borderColor;
}

- (UIColor *)borderColorDisabled {
    if (_borderColorDisabled)
        return _borderColorDisabled;
    else
        return self.borderColor;
}

#pragma mark -

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    if (self.state != UIControlStateDisabled && !self.isHighlighted)
        self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderColorPress:(UIColor *)borderColorPress {
    _borderColorPress = borderColorPress;
    if (self.state != UIControlStateDisabled && self.isHighlighted)
        self.layer.borderColor = borderColorPress.CGColor;
}

- (void)setBorderColorDisabled:(UIColor *)borderColorDisabled {
    _borderColorDisabled = borderColorDisabled;
    if (self.state == UIControlStateDisabled)
        self.layer.borderColor = borderColorDisabled.CGColor;
}

#pragma mark -

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderWidth = 0.5;
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.layer.borderWidth = 0.5;
    }
    return self;
}

//- (void)prepareForInterfaceBuilder {
//    [super prepareForInterfaceBuilder];
//    self.layer.borderWidth = 0.5;
//}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    if (self.state != UIControlStateDisabled)
        self.layer.borderColor = self.isHighlighted ? self.borderColorPress.CGColor : self.borderColor.CGColor;
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    if (self.state == UIControlStateDisabled)
        self.layer.borderColor = self.borderColorDisabled.CGColor;
    else
        self.layer.borderColor = self.isHighlighted ? self.borderColorPress.CGColor : self.borderColor.CGColor;
}

@end
